package com.anxin.act.task.complete.impl;

import com.alibaba.fastjson.JSONArray;
import com.anxin.act.task.cmd.CreateAndTakeTransitionCmd;
import com.anxin.act.task.cmd.DeleteTaskCmd;
import com.anxin.act.task.cmd.SequentialAndParallelCountersignAddcmd;
import com.anxin.act.task.complete.TaskComplete;
import com.anxin.act.task.dao.ActTaskDao;
import com.anxin.act.task.entity.WfActHiTask;
import com.anxin.act.task.entity.WfActRuTask;
import com.anxin.act.task.service.AddTaskService;
import com.anxin.act.task.service.WfActHiCommentService;
import com.anxin.act.task.service.WfActHiTaskService;
import com.anxin.act.task.service.WfActRuTaskService;
import com.anxin.act.task.vo.TaskVo;
import com.anxin.act.utils.ActTaskUtils;
import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.IdGen;
import com.anxin.common.utils.IdUtils;
import com.anxin.common.utils.sys.UserUtils;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.impl.RuntimeServiceImpl;
import org.activiti.engine.impl.bpmn.behavior.ParallelMultiInstanceBehavior;
import org.activiti.engine.impl.interceptor.CommandExecutor;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.task.Task;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.support.TaskUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前加签提交
 *
 * @author: liuxiangyu
 * @date: 2020/4/26 16:22
 */
@Service
public class AddBeforeCompleteImpl extends TaskComplete {

    @Autowired
    private AddTaskService addTaskService;

    @Autowired
    private WfActRuTaskService wfActRuTaskService;

    @Autowired
    private WfActHiTaskService wfActHiTaskService;

    @Autowired
    private WfActHiCommentService wfActHiCommentService;

    @Autowired
    private FreeFlowCompleteImpl freeFlowComplete;

    /**
     * 提交流程
     *
     * @param taskVo
     */
    public String complete(TaskVo taskVo) {


        // 保存当前任务的审批记录
        WfActHiTask wfActHiTask = wfActHiCommentService.getHiTask(taskVo);
        wfActHiTaskService.save(wfActHiTask);

        wfActHiCommentService.addComment(taskVo.getActId(),wfActHiTask.getId(), "前加签:" + UserUtils.getNickNamesByUserArray(taskVo.getAddUser()));
        wfActHiCommentService.addAddTaskComment(taskVo,wfActHiTask.getId());


        // 进行前加签
        wfActRuTaskService.addTask(taskVo);
        // 挂起流程
        addTaskService.hangTask(taskVo.getViewRuTask());

        // 如果是自由流前加签则把当前任务审批人放入后续审批人中
//        if (("actTask".equals(taskVo.getTaskType()) || "freeFlow".equals(taskVo.getTaskType()) ) && "act_free_flow_approval".equals(taskVo.getProcessDefKey())){
//            freeFlowComplete.addFreeFlowApproval(taskVo.getActId(),taskVo.getViewRuTask().getAssignee());
//        }

        return wfActHiTask.getId();

    }


    /**
     * 前加签
     */
    public ActivityImpl[] insertTasksBefore(TaskVo taskVo) {
        return addTaskService.cloneAndMakeChain(taskVo, "actBefore", taskVo.getTaskKey(), taskVo.getOpinions(), UserUtils.idToLoginName(taskVo.getAddUser()).toArray(new String[]{}));
    }
}
